<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<%--<c:set var="upmsAgents" value="${upmsAgents}"/>--%>

<div id="createDialog" class="container-fluid">
    <div class="row">
        <form id="createForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="classcode">
                    <span class="help-inline"><font color="red">*</font> </span>
                    产品分类：</label>
                <div class="col-sm-5">
                    <select id="classcode" name="classcode" class="form-control">
                        <c:forEach var="upmsDict" items="${classCodeList}">
                            <option value="${upmsDict.dicValue}">${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="version">
                    <span class="help-inline"><font color="red">*</font> </span>
                    代号：</label>
                <div class="col-sm-5">
                    <select id="version" name="version" class="form-control">
                        <c:forEach var="upmsDict" items="${machineTypeList}">
                            <option value="${upmsDict.dicValue}" selected="selected">${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="subversion">
                    <span class="help-inline"><font color="red">*</font> </span>
                    升级或变型代号：</label>
                <div class="col-sm-5">
                    <select id="subversion" name="subversion" class="form-control">
                        <c:forEach var="upmsDict" items="${subVersionList}">
                            <option value="${upmsDict.dicValue}">${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="years">
                    <span class="help-inline"><font color="red">*</font> </span>
                    年代号：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="years" name="years" placeholder="年代号" maxlength="2" value="${umbrellaHardwareProductSerialnumber.years}" onkeyup="value=value.replace(/[^\d]/g,'')" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="months">
                    <span class="help-inline"><font color="red">*</font> </span>
                    月份：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="months" name="months" placeholder="月份" maxlength="2" value="${umbrellaHardwareProductSerialnumber.months}"   onkeyup="value=value.replace(/[^\d]/g,'')"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="batchnumber">
                    <span class="help-inline"><font color="red">*</font> </span>
                    批次号：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="batchnumber" name="batchnumber" placeholder="批次号"maxlength="3" value="${umbrellaHardwareProductSerialnumber.batchnumber}"  onkeyup="value=value.replace(/[^\d]/g,'')"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="batchCount">
                    <span class="help-inline"><font color="red">*</font> </span>
                    批量生成数量：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="batchCount" name="batchCount" placeholder="批量生成数量" maxlength="3"  onkeyup="value=value.replace(/[^\d]/g,'')"/>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="createDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: {
                classcode: {
                    required: true
                },
                version: {
                    required: true
                },
                subversion: {
                    required: true
                },
                years: {
                    required: true,
                    number:true,
                    max:99,
                    min:0
                },
                months: {
                    required: true,
                    number:true,
                    max:12,
                    min:1
                },
                batchnumber: {
                    required: true,
                    number:true,
                    max:999,
                    min:1
                },
                batchCount: {
                    required: true,
                    number:true,
                    max:999,
                    min:1
                }
            },
            messages: {
                classcode: {
                    required: "产品分类是必选项。"
                },
                version: {
                    required: "型号代号是必选项。"
                },
                subversion: {
                    required: "换代或变型代号是必选项。"
                },
                years: {
                    required: "年代号是必填项。",
                    number:"请输入有效数字",
                    max:"最大限制为99",
                    min:"最小限制为0"
                },
                months: {
                    required: "月份是必填项。",
                    number:"请输入有效数字",
                    max:"最大限制为12",
                    min:"最小限制为1"
                },
                batchnumber: {
                    required: "批次号是必填项。",
                    number:"请输入有效数字",
                    max:"最大限制为999",
                    min:"最小限制为1"
                },
                batchCount: {
                    required: "批量生成数量是必填项。",
                    number:"请输入有效数字",
                    max:"最大限制为999",
                    min:"最小限制为1"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/serialnum/create',
                data: $('#createForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        createDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>